package com.jade.bss.web.controller;

import com.jade.bss.base.RoleAction;
import com.jade.bss.base.customer.*;
import com.jade.bss.base.customer.info.EnterpriseInfo;
import com.jade.framework.base.context.ApplicationContextUtils;
import com.jade.framework.base.criterion.Criteria;
import com.jade.framework.base.criterion.CriterionUtils;
import com.jade.framework.base.criterion.MatchMode;
import com.jade.framework.base.criterion.Restrictions;
import com.jade.framework.base.util.PagingList;
import com.jade.framework.base.util.PagingResult;
import com.jade.framework.base.util.ResponseUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: jinya
 * Date: 2017/6/29
 * Time: 16:04
 * TEL :17701640710   MAIL:2009_jinya@163.com
 * To change this template use File | Settings | File Templates.
 */
@RestController
@RequestMapping(value = "/enterprise", produces = MediaType.APPLICATION_JSON_VALUE)
@Api(value = "企业圈", description = "平台服务的企业")
public class EnterpriseController {
    private EnterpriseManager enterpriseManager = ApplicationContextUtils.getBean("bss_enterpriseInfoManager");
    @RequiresAuthentication
    @RequestMapping (value = "/bank", method = RequestMethod.GET)
    @ApiOperation(value = "资金提供方", httpMethod = "GET", response = CustomerAddress.class)
    public void query(@ApiParam(value = "关键词") @RequestParam(value = "keyword", required = false) String keyword,
                      @ApiParam(value = "地址类型") @RequestParam (value = "type", required = false) String type,
                      @ApiParam(value = "起始值") @RequestParam (value = "offset", defaultValue = "0") int offset,
                      @ApiParam(value = "每页大小") @RequestParam (value = "length", defaultValue = "10") int length,
                      @ApiParam(value = "排序字段") @RequestParam (value = "sort", required = false) String sortName,
                      @ApiParam(value = "排序规则", allowableValues = "desc, asc") @RequestParam (value = "order", required = false,defaultValue = "asc") String sortType,
                      HttpServletResponse response) throws Exception
    {
        Criteria criteria = new Criteria(offset, length);
        if (StringUtils.isNotBlank(keyword)) {
            CriterionUtils.addQuery(criteria, new String[]{"name","company","address","zipcode","mobile","email"}, keyword, MatchMode.ANYWHERE, null);
        }
        criteria.add(Restrictions.eq("type", EnterpriseInfo.TYPE_BANK));
        CriterionUtils.addOrder(criteria, sortName, sortType, "id");
        PagingList<EnterpriseInfo> addresses = enterpriseManager.query(criteria);
        PagingResult result = PagingResult.getResult(addresses);
        result.setOffset(offset);
        result.setPageSize(length);
        ResponseUtils.writeSuccessPagingResult(response, result);
    }

    @RequiresAuthentication
    @RequestMapping (value = RoleAction.GET, method = RequestMethod.GET)
    @ApiOperation(value = "获取企业信息", httpMethod = "GET")
    public void get(@ApiParam(value = "用户标识") @PathVariable long id, HttpServletResponse response)
            throws Exception
    {
        EnterpriseInfo customer = enterpriseManager.get(id);
        ResponseUtils.writeSuccessResult(response, customer);
    }
}
